package com.longcai.piaojia.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

/**
 * Created by Administrator on 2015/9/3 0003.
 */
public abstract class StartView extends LinearLayout {

    private OnItemSelectListener onItemSelectListener;

    public StartView(Context context, AttributeSet attrs) {

        super(context, attrs);

        for (int i = 0 ; i < max() ; i ++){

            View view = getView(new View(context));

            view.setTag(i);

            if(isClick()){

                view.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        setSelect((Integer) v.getTag());

                    }

                });

            }

            addView(view);

        }

        setSelect_def(0);

    }

    public void setSelect_def(int count){

        for (int i = 0 ; i < getChildCount() ; i ++){

            getChildAt(i).setBackgroundResource(selectNo());

        }

        if(onItemSelectListener != null){

            onItemSelectListener.onItemSelect(count);

        }

    }

    public void setSelect(int count){

        for (int i = 0 ; i < getChildCount() ; i ++){

            getChildAt(i).setBackgroundResource(i <= count ? selectYes() : selectNo());

        }

        if(onItemSelectListener != null){

            onItemSelectListener.onItemSelect(count);

        }

    }

    public void setOnItemSelectListener(OnItemSelectListener onItemSelectListener){

        this.onItemSelectListener = onItemSelectListener;

    }

    protected abstract boolean isClick();

    protected abstract int max();

    protected abstract int selectYes();

    protected abstract int selectNo();

    protected abstract View getView(View view);

    public interface OnItemSelectListener{

        void onItemSelect(int index);

    }

}
